#include <QCoreApplication>

#include "demuxthread.h"
#include "avpacketqueue.h"
#include "avframequeue.h"
#include "decodethread.h"
#include "audiooutput.h"
#include "videooutput.h"
#include "avsync.h"

#undef main
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int ret =0;

    //queue
    AVPacketQueue audio_packet_queue;
    AVPacketQueue video_packet_queue;

    AVFrameQueue audio_frame_queue;
    AVFrameQueue video_frame_queue;

    AVSync avsync;
    avsync.InitClock();


    ///1.解复用
    DemuxThread *demuxThread = new DemuxThread(&audio_packet_queue, &video_packet_queue);
    ret = demuxThread->Init("D:\\MV\\testvideo\\test.mov");
    if(ret != 0) {
        qDebug() <<" demuxThread.Init("") err "<< "";
    }

    ret = demuxThread->Start();
    if(ret != 0) {
        qDebug() <<" demuxThread.Start("") err "<< "";
    }

    //解码线程初始化
    DecodeThread *audio_decode_thread = new DecodeThread(&audio_packet_queue, &audio_frame_queue);

    ret = audio_decode_thread->Init(demuxThread->AudioCodecParameters());
    if(ret != 0) {
        qDebug() <<" audio_decode_thread.Init("") err "<< "";
    }

    ret = audio_decode_thread->Start();
    if(ret != 0) {
        qDebug() <<" audio_decode_thread.Start("") err "<< "";
    }

    DecodeThread *video_decode_thread = new DecodeThread(&video_packet_queue, &video_frame_queue);

    ret = video_decode_thread->Init(demuxThread->VideoCodecParameters());
    if(ret != 0) {
        qDebug() <<" video_decode_thread.Init("") err "<< "";
    }

    ret = video_decode_thread->Start();
    if(ret != 0) {
        qDebug() <<" video_decode_thread.Start("") err "<< "";
    }

    //初始化audio output
    AudioParams audioParams;
    memset(&audioParams, 0, sizeof(AudioParams));
    audioParams.channeles = demuxThread->AudioCodecParameters()->channels;
    audioParams.channel_layout = demuxThread->AudioCodecParameters()->channel_layout;
    audioParams.fmt = (enum AVSampleFormat)demuxThread->AudioCodecParameters()->format;
    audioParams.freq = demuxThread->AudioCodecParameters()->sample_rate;
    audioParams.frame_size = demuxThread->AudioCodecParameters()->frame_size;

    AudioOutput *audioOutput = new AudioOutput(&avsync, demuxThread->AudioStreamTimebase(), audioParams, &audio_frame_queue);
    ret = audioOutput->Init();
    if(ret != 0) {
        qDebug() <<" audioOutput->Init() err "<< "";
    }

    //初始化video output
    VideoOutput *videoOutput = new VideoOutput(&avsync, demuxThread->VideoStreamTimebase(), &video_frame_queue, demuxThread->VideoCodecParameters()->width, demuxThread->VideoCodecParameters()->height);
    ret = videoOutput->Init();
    if(ret != 0) {
        qDebug() <<" videoOutput->Init() err "<< "";
        return -1;
    }
    videoOutput->MainLoop();


    //休眠两秒；
//    std::this_thread::sleep_for(std::chrono::milliseconds(120 * 1000));
    demuxThread->Stop();
    delete demuxThread;

//    audio_decode_thread->Stop();
//    delete audio_decode_thread;

    video_decode_thread->Stop();
    delete video_decode_thread;
    qDebug() <<" endend "<< "";

    return a.exec();
}
